/*
 * Copyright (c) 2014 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.entity;

import it.polimi.se2.agostini_cantini_dignani.meteocal.model.DatePattern;
import it.polimi.se2.agostini_cantini_dignani.meteocal.model.TemperatureUnit;
import it.polimi.se2.agostini_cantini_dignani.meteocal.model.TimePattern;
import it.polimi.se2.agostini_cantini_dignani.meteocal.util.HashUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import java.io.Serializable;
import java.util.List;

/**
 * @author Alessandro Cantini
 */
@Entity(name = User.GROUP_NAME)
@XmlAccessorType(XmlAccessType.NONE)
public class User implements Serializable {

	public static final String GROUP_NAME = "USERS";
	private static final long serialVersionUID = 1L;

	@XmlAttribute
	@Id
	@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
			message = "Invalid email")
	@NotNull(message = "May not be empty")
	private String email;

	@NotNull(message = "May not be empty")
	private String password;

	@NotNull(message = "May not be empty")
	private String groupName;

	@OneToMany(orphanRemoval = true, mappedBy = "owner")
	private List<Notification> notifications;

	private boolean receivingEmail = false;

	@NotNull
	@Enumerated
	private TemperatureUnit temperatureUnit = TemperatureUnit.CELSIUS;

	@NotNull
	@Enumerated
	private DatePattern datePattern = DatePattern.WORLD;

	@NotNull
	@Enumerated
	private TimePattern timePattern = TimePattern.H24;

	@NotNull
	@OneToOne(cascade = CascadeType.PERSIST, orphanRemoval = true)
	private Calendar calendar;

	@OneToOne(mappedBy = "user", cascade = CascadeType.PERSIST, orphanRemoval = true)
	private Token token;

	public User() {
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = HashUtils.encryptPassword(password);
	}

	public List<Notification> getNotifications() {
		return notifications;
	}

	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;

		if (!equals(calendar.getOwner())) {
			calendar.setOwner(this);
		}
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public boolean isReceivingEmail() {
		return receivingEmail;
	}

	public void setReceivingEmail(boolean receivesMail) {
		if (receivesMail && !hasConfirmedEmail()) {
			throw new IllegalArgumentException("Cannot set email receiving on true if the user has not confirmed the email");
		}
		this.receivingEmail = receivesMail;
	}

	public TemperatureUnit getTemperatureUnit() {
		return temperatureUnit;
	}

	public void setTemperatureUnit(TemperatureUnit temperatureUnit) {
		this.temperatureUnit = temperatureUnit;
	}

	public DatePattern getDatePattern() {
		return datePattern;
	}

	public void setDatePattern(DatePattern datePattern) {
		this.datePattern = datePattern;
	}

	public TimePattern getTimePattern() {
		return timePattern;
	}

	public void setTimePattern(TimePattern timePattern) {
		this.timePattern = timePattern;
	}

	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		this.token = token;
	}

	public String getDateTimePattern() {
		return datePattern.getPattern() + " " + timePattern.getPattern();
	}

	public boolean hasConfirmedEmail() {
		return token == null;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (email != null ? email.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		}
		User other = (User) object;
		if ((this.email == null && other.email != null) || (this.email != null && !this.email.equals(other.email))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "User[ email=" + email + " ]";
	}
}
